﻿using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace HirelSeriesH
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非线程异常
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                WelcomeHelper.ShowLoadingScreen();
                bool flag = Initializer.initializSystem(true);
                //WelcomeHelper.CloseForm();
                if (flag)
                {
                    Application.Run(new HomeForm());
                }
                else
                {
                    UIMessageDialog.ShowMessageDialog("系统初始化失败", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                string str = "";
                string strDateInfo = DateTime.Now.ToString() + " 出现应用程序未处理的异常：" + "\r\n";

                if (ex != null)
                {
                    str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                         ex.GetType().Name, ex.Message, ex.StackTrace);
                }
                else
                {
                    str = string.Format("应用程序线程错误:{0}", ex);
                }
                WriteLog(str);
            }

        }

        /// <summary>
        /// 处理未捕获异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 未捕获的异常\r\n" + e.ExceptionObject.ToString());
            //此处可以弹出提示，退出程序
        }

        /// <summary>
        /// 处理UI主线程异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " UI主线程异常\r\n" + "Message：" + e.Exception.Message + "\r\n" + "StackTrace：" + e.Exception.StackTrace);
            //此处可以弹出提示，退出程序
        }

        public static void WriteLog(string log)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + @"\ErrLog.log";

            FileStream fileStream;
            if (!File.Exists(filePath))
            {
                //创建写入文本文件
                fileStream = new FileStream(filePath, FileMode.Create);
                fileStream.Close();
            }

            using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.WriteLine(log);
                }
            }
        }

    }
}
